﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UdfReportSettingDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUdfReportSettingDetailDA udfReportSettingDetailDA = DataAccess.CreateUdfReportSettingDetailDA();

        public UdfReportSettingDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public UdfReportSettingDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UdfReportSettingDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1870_002", this.pubParasInfo.UserLanguage);
                }
                List<UdfReportSettingDetailInfo> UdfReportSettingDetailInfos = new List<UdfReportSettingDetailInfo>();
                UdfReportSettingDetailInfo UdfReportSettingDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    UdfReportSettingDetailInfo = udfReportSettingDetailDA.GetUdfReportSettingDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0], this.pubParasInfo);
                    UdfReportSettingDetailInfos.Add(UdfReportSettingDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (udfReportSettingDetailDA.DeleteUdfReportSettingDetail(dataBase, tran, mainID, subId, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<UdfReportSettingDetailInfo>(dataBase, tran, UdfReportSettingDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return udfReportSettingDetailDA.GetAllUdfReportSettingDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            UdfReportSettingDetailQueryEntity UdfReportSettingDetail = obj as UdfReportSettingDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.ReportID))
            {
                sqlWhere.Add(" REPORT_ID " + UdfReportSettingDetail.ReportID);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.LineID.ToString()))
            {
                sqlWhere.Add(" LINE_ID " + UdfReportSettingDetail.LineID.ToString());
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.CtrlID))
            {
                sqlWhere.Add(" CTRL_ID " + UdfReportSettingDetail.CtrlID);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.CtrlLable))
            {
                sqlWhere.Add(" CTRL_LABEL " + UdfReportSettingDetail.CtrlLable);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.CtrlType))
            {
                sqlWhere.Add(" CTRL_TYPE " + UdfReportSettingDetail.CtrlType);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.DefaultValue))
            {
                sqlWhere.Add(" DEFAULT_VALUE " + UdfReportSettingDetail.DefaultValue);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.ParamName))
            {
                sqlWhere.Add(" PARAM_NAME " + UdfReportSettingDetail.ParamName);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.IsMustInput))
            {
                sqlWhere.Add(" IS_MUST_INPUT " + UdfReportSettingDetail.IsMustInput);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.Udf1))
            {
                sqlWhere.Add(" UDF1 " + UdfReportSettingDetail.Udf1);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.Udf2))
            {
                sqlWhere.Add(" UDF2 " + UdfReportSettingDetail.Udf2);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.Udf3))
            {
                sqlWhere.Add(" UDF3 " + UdfReportSettingDetail.Udf3);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.Udf4))
            {
                sqlWhere.Add(" UDF4 " + UdfReportSettingDetail.Udf4);
            }
            if (!string.IsNullOrEmpty(UdfReportSettingDetail.Udf5))
            {
                sqlWhere.Add(" UDF5 " + UdfReportSettingDetail.Udf5);
            }
            return udfReportSettingDetailDA.GetUdfReportSettingDetailByQueryList(sqlWhere, UdfReportSettingDetail, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return udfReportSettingDetailDA.GetUdfReportSettingDetailByID(mainID, subID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<UdfReportSettingDetailInfo> UdfReportSettingDetail = udfReportSettingDetailDA.GetUdfReportSettingDetailListByID(mainID, this.dataBaseID[0], this.pubParasInfo);
            List<object> objs = null;
            if ((UdfReportSettingDetail != null) && (UdfReportSettingDetail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < UdfReportSettingDetail.Count; i++)
                {
                    objs.Add(UdfReportSettingDetail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            UdfReportSettingDetailInfo UdfReportSettingDetailInfo = obj as UdfReportSettingDetailInfo;
            string LineID = string.Empty;
            try
            {
                if (udfReportSettingDetailDA.CheckUdfReportSettingDetailIDUnique(UdfReportSettingDetailInfo.ReportID, UdfReportSettingDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1870_001", this.pubParasInfo.UserLanguage, new List<string> { UdfReportSettingDetailInfo.ReportID, UdfReportSettingDetailInfo.LineID.ToString() });
                }
                if (UdfReportSettingDetailInfo == null)
                {
                    return LineID;
                }
                UdfReportSettingDetailInfo.UpdatedBy = UdfReportSettingDetailInfo.CreatedBy;
                UdfReportSettingDetailInfo.CreatedDate = DateTime.Now;
                UdfReportSettingDetailInfo.UpdatedDate = DateTime.Now;
                if (udfReportSettingDetailDA.InsertUdfReportSettingDetail(obj as UdfReportSettingDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UdfReportSettingDetailInfo>(null, UdfReportSettingDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                    LineID = UdfReportSettingDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                UdfReportSettingDetailInfo UdfReportSettingDetailInfo = obj as UdfReportSettingDetailInfo;
                UdfReportSettingDetailInfo oldUdfReportSettingDetailInfo = udfReportSettingDetailDA.GetUdfReportSettingDetailByID(UdfReportSettingDetailInfo.ReportID, UdfReportSettingDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (udfReportSettingDetailDA.UpdateUdfReportSettingDetail(UdfReportSettingDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UdfReportSettingDetailInfo>(oldUdfReportSettingDetailInfo, UdfReportSettingDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                    LineID = UdfReportSettingDetailInfo.ReportID + " , " + UdfReportSettingDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

